{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Extension Command Tests\n",
    "\n",
    "- Run the first block to create the command test chain.\n",
    "- Run the second block to create the chain.\n",
    "- Run the third block to run the chain.\n",
    "\n",
    "### Create the chain function\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from ApiClient import ApiClient\n",
    "\n",
    "\n",
    "def create_command_test_chain(\n",
    "    chain_name=\"Test Commands\", agent_name=\"gpt4free\", user_input=\"Test value\"\n",
    "):\n",
    "    command_list = ApiClient.get_extensions()\n",
    "    result = {\"chain_name\": chain_name, \"steps\": []}\n",
    "    order = [\n",
    "        \"Write to File\",\n",
    "        \"Append to File\",\n",
    "        \"Read File\",\n",
    "        \"Search Files\",\n",
    "        \"Delete File\",\n",
    "        \"Clone Github Repository\",\n",
    "        \"Analyze Pull Request\",\n",
    "        \"Perform Automated Testing\",\n",
    "        \"Run CI-CD Pipeline\",\n",
    "        \"Improve Code\",\n",
    "        \"Write Tests\",\n",
    "        \"Evaluate Code\",\n",
    "        \"Execute Python File\",\n",
    "        \"Execute Shell\",\n",
    "        \"Scrape Text with Playwright\",\n",
    "        \"Scrape Links with Playwright\",\n",
    "        \"Speak with MacOS TTS\",\n",
    "        \"Speak with TTS with BrianTTS\",\n",
    "        \"Speak with TTS Using Elevenlabs\",\n",
    "        \"Read Audio from File\",\n",
    "        \"Read Audio\",\n",
    "        \"Generate Image with Stable Diffusion\",\n",
    "        \"Get Datetime\",\n",
    "        \"Use The Search Engine\",\n",
    "    ]\n",
    "    for i, item in enumerate(command_list):\n",
    "        if \"Ask AI\" not in item[0] and \"Instruct AI\" not in item[0]:\n",
    "            if \"agent\" not in item[2].keys():\n",
    "                step = {\n",
    "                    \"step\": i + 1,\n",
    "                    \"agent_name\": agent_name,\n",
    "                    \"prompt_type\": \"Command\",\n",
    "                    \"prompt\": {\"command_name\": item[0]},\n",
    "                }\n",
    "                # Assign Test values to the command parameters.\n",
    "                for key in item[2].keys():\n",
    "                    if key == \"text\":\n",
    "                        if item[0] == \"Write to File\" or item[0] == \"Append to File\":\n",
    "                            step[\"prompt\"][key] = \"print('Hello, world!')\"\n",
    "                        else:\n",
    "                            step[\"prompt\"][key] = \"This is test text.\"\n",
    "                    elif key == \"code\":\n",
    "                        step[\"prompt\"][key] = \"print('Hello, world!')\"\n",
    "                    elif key == \"url\":\n",
    "                        step[\"prompt\"][\n",
    "                            key\n",
    "                        ] = \"https://josh-xt.github.io/AGiXT/2-Concepts/0-Core%20Concepts.html\"\n",
    "                    elif key == \"function_description\":\n",
    "                        step[\"prompt\"][key] = \"Hello World\"\n",
    "                    elif key == \"query\":\n",
    "                        step[\"prompt\"][\n",
    "                            key\n",
    "                        ] = \"What is the capital of the United States?\"\n",
    "                    elif key == \"audio_path\":\n",
    "                        step[\"prompt\"][key] = \"test_audio.wav\"\n",
    "                    elif key == \"file\" or key == \"filename\":\n",
    "                        if item[0] == \"Generate Image with Stable Diffusion\":\n",
    "                            step[\"prompt\"][key] = \"test_image.png\"\n",
    "                        else:\n",
    "                            step[\"prompt\"][key] = \"hello_world.py\"\n",
    "                    elif key == \"directory\":\n",
    "                        step[\"prompt\"][key] = \".\"\n",
    "                    elif key == \"clone_path\":\n",
    "                        step[\"prompt\"][key] = \"AGiXT\"\n",
    "                    elif key == \"command_line\":\n",
    "                        step[\"prompt\"][key] = \"echo 'Hello, world!'\"\n",
    "                    elif key == \"voice_index\":\n",
    "                        step[\"prompt\"][key] = 0\n",
    "                    elif key == \"repo_url\":\n",
    "                        step[\"prompt\"][key] = \"https://github.com/Josh-XT/AGiXT\"\n",
    "                    elif key == \"prompt\":\n",
    "                        step[\"prompt\"][key] = \"Picture of a dragon.\"\n",
    "                    else:\n",
    "                        step[\"prompt\"][key] = user_input\n",
    "            result[\"steps\"].append(step)\n",
    "\n",
    "    result[\"steps\"] = sorted(\n",
    "        result[\"steps\"],\n",
    "        key=lambda x: order.index(x[\"prompt\"][\"command_name\"])\n",
    "        if x[\"prompt\"][\"command_name\"] in order\n",
    "        else len(order),\n",
    "    )\n",
    "    for i, step in enumerate(result[\"steps\"], start=1):\n",
    "        step[\"step\"] = i\n",
    "    # Create the chain\n",
    "    ApiClient.add_chain(chain_name=chain_name)\n",
    "    # Add the steps to the chain\n",
    "    for step in result[\"steps\"]:\n",
    "        ApiClient.add_step(\n",
    "            chain_name=chain_name,\n",
    "            step_number=step[\"step\"],\n",
    "            agent_name=agent_name,\n",
    "            prompt_type=step[\"prompt_type\"],\n",
    "            prompt=step[\"prompt\"],\n",
    "        )\n",
    "    return result"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Make a chain called Test Commands of all commands.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "chain_name = \"Test Commands\"\n",
    "agent_name = \"gpt4free\"\n",
    "# Change user input to set the \"else\" on the get_commands_tests function\n",
    "# This will update the input value for anything that isn't already predefined.\n",
    "user_input = \"Test value\"\n",
    "\n",
    "commands = create_command_test_chain(\n",
    "    chain_name=chain_name, agent_name=agent_name, user_input=user_input\n",
    ")\n"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Run the chain.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from ApiClient import ApiClient\n",
    "\n",
    "# Change user input to set the \"else\" on the get_commands_tests function\n",
    "# This will update the input value for anything that isn't already predefined.\n",
    "chain_name = \"Test Commands\"\n",
    "user_input = \"Test value\"\n",
    "\n",
    "ApiClient.run_chain(chain_name=chain_name, user_input=user_input)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
